package cn.itcast.controller;

import cn.itcast.pojo.User;
import cn.itcast.pojo.UserVO;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.servlet.http.*;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings("all")
@Controller
//@RequestMapping("hello")
public class HelloController {
    /**
     * @RequestMapping("/show1.do"): 使得请求的url可以映射到指定的目标方法上
     * @return
     */
 //* @RequestMapping("/show1.do")
    public ModelAndView test(){
        ModelAndView mv=new ModelAndView();
        //设置要显示的页面：视图
        mv.setViewName("/WEB-INF/views/hello.jsp");
        //添加要显示的数据:数据是存在request域中
        mv.addObject("msg","这是springmvc第一个程序！");
        return mv;
    }
    @RequestMapping("show2")
    public String test2(Model model, @RequestParam(value = "name")String name){
        model.addAttribute("msg","使用@RequestParam接收到的参数为："+name);
        return "hello";
    }
    @RequestMapping("show4")
    public String test4(Model model, @RequestParam(value = "name",required=false)String name){
        model.addAttribute("msg","使用@RequestParam接收到的参数为："+name);
        return "hello";
    }
    @RequestMapping("show5")
    public String test5(Model model, @RequestParam(value = "name",defaultValue = "lisi")String name){
        model.addAttribute("msg","使用@RequestParam接收到的参数为："+name);
        return "hello";
    }
    @RequestMapping("show3")
    public String test2(Model model, HttpServletRequest request, HttpServletResponse response, HttpSession session){
        model.addAttribute("msg",request+"</br>"+response+"</br>"+session);
        return "hello";
    }
    @RequestMapping("show6")
    public String test6(Model model, HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equalsIgnoreCase("jsessionid")){
                model.addAttribute("msg","jsessionid:"+cookie.getValue());
            }
        }
        return "hello";
    }
    @RequestMapping("show7")
    public String test7(Model model, @CookieValue(value = "JSESSIONID",required = false)String jsessionid){
        model.addAttribute("msg","jsessionid:"+jsessionid);
        return "hello";
    }
    @RequestMapping("show8")
    public String test8(Model model, @RequestParam("name") String name,
                         @RequestParam("age") Integer age,
                         @RequestParam("isMarry") Boolean isMarry,
                         @RequestParam("income") Float income,
                         @RequestParam("interests") String[] interests) {

        System.out.println(name);
        System.out.println(age);
        System.out.println(isMarry);
        System.out.println(income);
        System.out.println(Arrays.toString(interests));

        return "hello";
    }

    @RequestMapping("show10")
    public String test10(Model model, User user) {
        model.addAttribute("msg",user);
        return "hello";
    }
    @RequestMapping("show11")
    public String test11(Model model, UserVO userVO){
        model.addAttribute("msg",userVO.toString());
        System.out.println(userVO);
        return "hello";
    }
    @RequestMapping(value="show9")
    public String test9(Model model) {
        List<User> list = new ArrayList<>();
        for(int i = 0;i< 20;i++) {
            User user = new User();
            user.setId(i+1L);
            user.setUsername("zhangsan"+i);
            user.setName("张三"+i);
            user.setAge(18);
            list.add(user);
        }
        model.addAttribute("users", list);
        return "userList";
    }

    @RequestMapping("show12")
    @ResponseBody
    public List<User> test12(){
        List<User> list = new ArrayList<User>();
        for(int i = 0;i<10;i++) {
            User user = new User();
            user.setId(i+1L);
            user.setUsername("zhangsan"+i);
            user.setName("张三"+i);
            user.setAge(18);
            list.add(user);
        }
        System.out.println(list);
        return list;
    }
 /*
     * 文件上传
     * 需要通过MultipartFile类型来接收数据
     *
     * @throws Exception
     * @throws IllegalStateException
   */
    @RequestMapping("show31")
    public String test31(Model model,@RequestParam("file")MultipartFile file) throws Exception{

        if (file!=null) {
            file.transferTo(new File("d://upload//"+file.getOriginalFilename()));
        }
        model.addAttribute("msg", "上传成功!");
        return "hello";
    }
    @RequestMapping(value="show28")
    @ResponseBody//将数据响应成json格式的数据
    public List<User> test28(Model model) {

        List<User> list = new ArrayList<User>();
        for(int i = 0;i< 20;i++) {
            User user = new User();
            user.setId(i+1L);
            user.setUsername("zhangsan"+i);
            user.setName("张三"+i);
            user.setAge(18);
            list.add(user);
        }
        return list;
    }
    @RequestMapping("demo1")
    @ResponseBody
    public List<User> demo1(){
        List<User> list = new ArrayList<User>();
        for(int i = 0;i< 20;i++) {
            User user = new User();
            user.setId(i+1L);
            user.setUsername("zhangsan"+i);
            user.setName("张三"+i);
            user.setAge(18);
            list.add(user);
        }
        return list;
    }
    @RequestMapping("show29")
    public String test29(Model model, @RequestBody()User user){
        model.addAttribute("msg",user);
        return "hello";
    }
    @RequestMapping("show30")
    public String test30(Model model, @RequestBody()String user){
        model.addAttribute("msg",user);
        return "hello";
    }
    @RequestMapping("show32")
    @ResponseStatus(HttpStatus.OK)
    public void test32(){
        System.out.println("contorller的test31方法执行了");
    }
    @RequestMapping("show33")
    public String test33(){
        System.out.println("show33的forward");
        return "forward:show34.do?type=forward";
    }
    @RequestMapping("show34")
    public String test34(Model model,@RequestParam("type")String type){
        model.addAttribute("msg",type);

        return "hello";
    }
    @RequestMapping("show35")
    public String tesst35(){
        System.out.println("35方法中的redirect");
        return "redirect:show34.do?type=redirect";
    }
}
